package com.example.as_sum.activity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Toast;

import com.example.as_sum.R;
import com.example.as_sum.permission.PermissionsUtil;
import com.example.as_sum.util.Log;

public class PermissionActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitle("权限测试");
    }

    @Override
    public int getContentId() {
        return R.layout.activity_permission;
    }
    public void readFile(View view){
        int hasWriteContactsPermission = 0;
        hasWriteContactsPermission = ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE);
        //PackageManager.PERMISSION_GRANTED=0   表示允许
        //PackageManager.PERMISSION_GRANTED=-1  被拒绝
        if(hasWriteContactsPermission==0)
            Log.e("checkSelfPermission","==(0)===>允许");
        else if(hasWriteContactsPermission==-1)
            Log.e("checkSelfPermission","===(-1)===>拒绝");
        if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
            //shouldShowRequestPermissionRationale第一次申请权限和用户单击不在提醒并且拒绝的时候，返回false
            //一直单击拒绝一直返回true(不要选中不再提醒)
            boolean ch=ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE);
            if(ch)
                Log.e("shouldShowRequestPermissionRationale","=====>true");
            else
                Log.e("shouldShowRequestPermissionRationale","======>false");
            if (!ch) {
                new AlertDialog.Builder(this)
                        .setMessage("提示用户要开启这个权限，否则会影响你的使用？")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                showShortMsg("你单机了取消");
                            }
                        })
                        .setPositiveButton("好的", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //异步方法
                                ActivityCompat.requestPermissions(PermissionActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                                        READ_EXTERNAL_PERMISSIONS);

                            }
                        }).create().show();
                return;
            }
            ActivityCompat.requestPermissions(PermissionActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    PHONE_PERMISSIONS);
            return;
        }
        insertDummyContact("开始读取SD卡数据");
    }

    private void insertDummyContact(String code) {
        Toast.makeText(PermissionActivity.this, code, Toast.LENGTH_SHORT)
                .show();
    }

    public void writeFile(View view){
        boolean check=PermissionsUtil.checkPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if(check){
            ActivityCompat.requestPermissions(PermissionActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    WRITE_EXTERNAL_PERMISSIONS);
        }else{
            insertDummyContact("开始往SD卡中写数据");
        }
    }
    public void readPhone(View view){
    }
    public void writePhone(View view){
    }
    @Override
    public void initView() {

    }

    @Override
    public void addListener() {

    }

    @Override
    public void loadData() {

    }

    @Override
    public Handler getHandler() {
        return null;
    }

}
